<template>
  <div>
    <div style="margin-bottom: 10px;" class="btn-group">
      <div>
        <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        <el-button type="primary" icon="el-icon-plus" @click="add">新增</el-button>
      </div>
    </div>
    <lb-table
      ref="lbTable"
      v-el-height-adaptive-table="{bottomOffset:10}"
      border
      highlight-current-row
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      pagination
      :total="tableData.total"
      :immediate-fetch="false"
      custom-column
      @fetch="fetchData"
    />
    <add ref="add" />
  </div>
</template>

<script>
  import { goodsSpecAliasDelete, queryGoodsSpecAliasList } from '@/api'
  import Add from './Add'

  export default {
    name: 'size',
    components: {
      Add
    },
    props: {
      currentGoodsType: {
        type: Object
      }
    },
    data() {
      return {
        tableData: {
          total: 0,
          column: [
            { type: 'index' },
            { label: '编码', prop: 'specAliasCode' },
            { label: '规格', prop: 'specAlias' },
            { label: '规格状态', prop: 'specStatus', formatter: this.specStatusFormat },
            { label: '备注', prop: 'remark' },
            {
              label: '操作',
              align: 'center',
              fixed: 'right',
              width: 85,
              render: (h, { row, $index }) => {
                return (
                  <div>
                    <el-link underline={false} size='mini' onClick={() => this.update(row, $index)}>
                      编辑
                    </el-link>
                    <el-link underline={false} size='mini' type='danger' onClick={() => this.del(row, $index)}>
                      删除
                    </el-link>
                  </div>
                )
              }
            }
          ],
          data: []
        }
      }
    },
    methods: {
      // 列表查询
      getTableData() {
        this.$nextTick(async() => {
          this.$refs.lbTable.fetch()
        })
      },
      async fetchData({ currentPage, pageSize }) {
        const res = await queryGoodsSpecAliasList({
          gtId: this.currentGoodsType.gtId,
          currentPage,
          pageSize
        })
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      specStatusFormat: function(row, column) {
        const val = row[column.property]
        if (val === -1) {
          return '作废'
        } else if (val === 2) {
          return '正常'
        }
        return '-'
      },
      async add() {
        await this.$refs.add.open()
        this.search()
      },
      update(row) {
        this.$refs.add.open(row)
        this.search()
      },
      del(row) {
        this.$confirm('确认要删除该商品规格？', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$message({
            message: '正在删除',
            duration: 0,
            iconClass: 'el-icon-loading'
          })
          goodsSpecAliasDelete({ specAliasId: row.specAliasId }).then(() => {
            this.$refs.lbTable.fetch()
            this.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          }).finally(() => {
            loading.close()
          })
        })
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .btn-group {
    display: flex;
    justify-content: space-between;
  }
</style>
